package com.company.backendproject.controller;

import com.alibaba.fastjson.JSON;
import com.company.backendproject.entity.Book;
import com.company.backendproject.entity.Page;
import com.company.backendproject.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author : Real
 * @date : 2022/1/13 15:12
 * @description : 书籍 Controller 层
 */
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping(path = "/book/list", method = RequestMethod.GET)
    @ResponseBody
    public String getBookList() {
        List<Book> bookList = bookService.findAllBook();
        return JSON.toJSONString(bookList);
    }

    @RequestMapping(path = "/book/{page}/{size}", method = RequestMethod.GET)
    @ResponseBody
    public String getBookPage(@PathVariable("page") Integer pageNumber, @PathVariable("size") Integer size, Page page) {
        List<Book> allBook = bookService.findAllBook();
        List<Book> bookList = bookService.findBookPage((pageNumber - 1) * size, size);
        int row = allBook.size();
        page.setRows(row);
        page.setCurrent(pageNumber);
        page.setLimit(size);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("content", bookList);
        jsonMap.put("page", page);
        return JSON.toJSONString(jsonMap);
    }

    @RequestMapping(path = "/book/save", method = RequestMethod.POST)
    @ResponseBody
    public String saveBook(@RequestBody Book book) {
        book.setAbled(1);
        book.setBookcaseid(new Random().nextInt(10));
        int i = bookService.saveBook(book);
        Map<String, Object> map = new HashMap<>(1);
        if (i == 1) {
            map.put("result", "success");
        } else {
            map.put("result", "failed");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(path = "/book/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String selectBookById(@PathVariable("id") int id) {
        Map<String, Object> map = new HashMap<>(2);
        if (StringUtils.isEmpty(id)) {
            map.put("result", "Error");
            return JSON.toJSONString(map);
        }
        Book book = bookService.findBookById(id);
        if (book == null) {
            map.put("result", "Result is empty.");
            return JSON.toJSONString(map);
        }
        map.put("result", "OK");
        map.put("content", book);
        return JSON.toJSONString(map);
    }

    @RequestMapping(path = "/book/update", method = RequestMethod.POST)
    @ResponseBody
    public String updateBookById(@RequestBody Book book) {
        Map<String, Object> map = new HashMap<>(1);
        if (book == null) {
            map.put("result", "Argument Error.");
            return JSON.toJSONString(map);
        }
        int i = bookService.updateBook(book);
        if (i == 1) {
            map.put("result", "success");
        } else {
            map.put("result", "failed");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(path = "/book/delete", method = RequestMethod.POST)
    @ResponseBody
    public String deleteBookById(@RequestBody Book book) {
        Map<String, Object> map = new HashMap<>(1);
        if (book == null) {
            map.put("result", "Argument Error.");
            return JSON.toJSONString(map);
        }
        int i = bookService.deleteBookById(book.getId());
        if (i == 1) {
            map.put("result", "success");
        } else {
            map.put("result", "failed");
        }
        return JSON.toJSONString(map);
    }
}
